/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.prolificinteractive.materialcalendarview.sample;

import ohos.aafwk.content.Intent;
import ohos.agp.components.AbsButton;
import ohos.agp.components.Button;
import ohos.agp.components.Checkbox;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentState;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.RadioButton;
import ohos.agp.components.RadioContainer;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.components.element.StateElement;
import ohos.agp.window.dialog.CommonDialog;

import com.prolificinteractive.materialcalendarview.CalendarDay;
import com.prolificinteractive.materialcalendarview.CalendarMode;
import com.prolificinteractive.materialcalendarview.MaterialCalendarView;
import com.prolificinteractive.materialcalendarview.sample.tools.ToastsUtils;

import java.util.List;

/**
 * @since 2021-07-12
 */
public class GettersAbility extends BaseAbility implements Component.ClickedListener {

    public static final CharSequence[] ITEMS =
        new CharSequence[] { "NONE", "SINGLE", "MULTIPLE", "RANGE" };

    private Button bt_get_current_date;
    private Button bt_get_selected_date;
    private Button bt_get_selected_dates;
    private Button bt_button_selection_mode;
    private Button bt_get_selection_mode;

    private CommonDialog commonDialog;
    private Checkbox ck;

    private MaterialCalendarView widget;


    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        toolbarTitle.setText("Calendar with Getters");
        widget = (MaterialCalendarView) findComponentById(ResourceTable.Id_calendarView);
        widget.setSelectionColor(COLOR_ACCENT);
        bt_get_current_date = (Button) findComponentById(ResourceTable.Id_get_current_date);
        bt_get_current_date.setClickedListener(this);
        bt_get_selected_date = (Button) findComponentById(ResourceTable.Id_get_selected_date);
        bt_get_selected_date.setClickedListener(this);
        bt_get_selected_dates = (Button) findComponentById(ResourceTable.Id_get_selected_dates);
        bt_get_selected_dates.setClickedListener(this);
        bt_button_selection_mode = (Button) findComponentById(ResourceTable.Id_button_selection_mode);
        bt_button_selection_mode.setClickedListener(this);
        bt_get_selection_mode = (Button) findComponentById(ResourceTable.Id_get_selection_mode);
        bt_get_selection_mode.setClickedListener(this);

        ck = (Checkbox) findComponentById(ResourceTable.Id_calendar_mode);
        ck.setCheckedStateChangedListener(new AbsButton.CheckedStateChangedListener() {
            @Override
            public void onCheckedChanged(AbsButton absButton, boolean b) {
                final CalendarMode mode = b ? CalendarMode.WEEKS : CalendarMode.MONTHS;
                widget.state().edit().setCalendarDisplayMode(mode).commit();
            }
        });
        initcommondialog();
    }

    @Override
    int contentLayoutRes() {
        return ResourceTable.Layout_ability_getters;
    }

    @Override
    public void onClick(Component v) {
        switch (v.getId()) {
            case ResourceTable.Id_get_current_date:
                new ToastsUtils.Builder(getContext())
                    .setToastText(widget.getCurrentDate().toString())
                    .build().show();
                break;
            case ResourceTable.Id_get_selected_date:
                final CalendarDay selectedDate = widget.getSelectedDate();
                if (selectedDate != null) {
                    new ToastsUtils.Builder(getContext())
                        .setToastText(selectedDate.toString())
                        .build().show();
                } else {
                    new ToastsUtils.Builder(getContext())
                        .setToastText("No Selection")
                        .build().show();
                }
                break;
            case ResourceTable.Id_get_selected_dates:
                final List<CalendarDay> selectedDates = widget.getSelectedDates();
                if (!selectedDates.isEmpty()) {
                    new ToastsUtils.Builder(getContext())
                        .setToastText(selectedDates.toString())
                        .build().show();
                } else {
                    new ToastsUtils.Builder(getContext())
                        .setToastText("No Selection")
                        .build().show();
                }
                break;
            case ResourceTable.Id_button_selection_mode:
//                showcommondialog();
                commonDialog.show();
                break;
            case ResourceTable.Id_get_selection_mode:
                new ToastsUtils.Builder(getContext())
                    .setToastText(widget.getSelectionMode().name().substring(15,widget.getSelectionMode().name().length()))
                    .build().show();

                break;
            default:
                throw new IllegalStateException("Unexpected value: ");
        }
    }

    private void initcommondialog() {
        RadioContainer mRadioContainer;
        Component mComponent = LayoutScatter.getInstance(getContext())
            .parse(ResourceTable.Layout_commondialog_getters, null, false);
        commonDialog = initdialog(mComponent, this);
        StateElement mstateNone=new StateElement();
        mstateNone.addState(new int[]{ComponentState.COMPONENT_STATE_CHECKED}, new PixelMapElement(getPixelMap(ResourceTable.Media_baseline_radio_button_checked_black_24dp)));
        mstateNone.addState(new int[]{ComponentState.COMPONENT_STATE_EMPTY},new PixelMapElement(getPixelMap(ResourceTable.Media_baseline_radio_button_unchecked_black_24dp)));
        StateElement mstateSingle=new StateElement();
        mstateSingle.addState(new int[]{ComponentState.COMPONENT_STATE_CHECKED}, new PixelMapElement(getPixelMap(ResourceTable.Media_baseline_radio_button_checked_black_24dp)));
        mstateSingle.addState(new int[]{ComponentState.COMPONENT_STATE_EMPTY},new PixelMapElement(getPixelMap(ResourceTable.Media_baseline_radio_button_unchecked_black_24dp)));
        StateElement mstateMultiple=new StateElement();
        mstateMultiple.addState(new int[]{ComponentState.COMPONENT_STATE_CHECKED}, new PixelMapElement(getPixelMap(ResourceTable.Media_baseline_radio_button_checked_black_24dp)));
        mstateMultiple.addState(new int[]{ComponentState.COMPONENT_STATE_EMPTY},new PixelMapElement(getPixelMap(ResourceTable.Media_baseline_radio_button_unchecked_black_24dp)));
        StateElement mstateRange=new StateElement();
        mstateRange.addState(new int[]{ComponentState.COMPONENT_STATE_CHECKED}, new PixelMapElement(getPixelMap(ResourceTable.Media_baseline_radio_button_checked_black_24dp)));
        mstateRange.addState(new int[]{ComponentState.COMPONENT_STATE_EMPTY},new PixelMapElement(getPixelMap(ResourceTable.Media_baseline_radio_button_unchecked_black_24dp)));
        RadioButton radioNone=(RadioButton) mComponent.findComponentById(ResourceTable.Id_action_empty);
        RadioButton radioSingle=(RadioButton) mComponent.findComponentById(ResourceTable.Id_action_loading);
        RadioButton radioMultiple=(RadioButton) mComponent.findComponentById(ResourceTable.Id_action_error);
        RadioButton radioRange=(RadioButton) mComponent.findComponentById(ResourceTable.Id_action_content);
        radioNone.setButtonElement(mstateNone);
        radioSingle.setButtonElement(mstateSingle);
        radioMultiple.setButtonElement(mstateMultiple);
        radioRange.setButtonElement(mstateRange);
        radioSingle.setChecked(true);
        mRadioContainer = (RadioContainer) mComponent.findComponentById(ResourceTable.Id_rc_group);
        mRadioContainer.setMarkChangedListener(new RadioContainer.CheckedStateChangedListener() {
            @Override
            public void onCheckedChanged(RadioContainer radioContainer, int i) {
                if (i == 1) {
                    widget.setSelectionMode(MaterialCalendarView.SelectionMode.SELECTION_MODE_NONE);
                    commonDialog.hide();
                } else if (i == 2) {
                    widget.setSelectionMode(MaterialCalendarView.SelectionMode.SELECTION_MODE_SINGLE);
                    commonDialog.hide();
                } else if (i == 3) {
                    widget.setSelectionMode(MaterialCalendarView.SelectionMode.SELECTION_MODE_MULTIPLE);
                    commonDialog.hide();
                } else if (i == 4) {
                    widget.setSelectionMode(MaterialCalendarView.SelectionMode.SELECTION_MODE_RANGE);
                    commonDialog.hide();
                } else {
                    throw new IllegalStateException("Unexpected value: ");
                }

            }
        });
        DependentLayout mDependentLayout = (DependentLayout) mComponent.findComponentById(ResourceTable.Id_mDirLayout);
        mDependentLayout.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component v) {
                commonDialog.hide();
            }
        });
    }

}
